package com.katze.boot.system.controller;

import com.katze.boot.common.exception.BusinessException;
import com.katze.boot.plugins.security.annotations.RestControllerMapping;
import com.katze.boot.plugins.security.annotations.RestRequestMapping;
import com.katze.boot.system.domain.Dept;
import com.katze.boot.system.service.DeptService;
import com.katze.common.Message;
import com.katze.common.controller.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@RestControllerMapping("/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RestRequestMapping(permits = "dept:view", skip = true)
    public Message deptList(Dept dept) {
        return Message.ok().data(this.deptService.findDepts(dept));
    }

    @RestRequestMapping(method = RequestMethod.POST, permits = "dept:add", description = "新增部门")
    public Message addDept(Dept dept) {
        this.deptService.createDept(dept);
        return Message.ok("新增部门成功");
    }

    @RestRequestMapping(method = RequestMethod.DELETE, permits = "dept:delete", description = "删除部门")
    public Message deleteDepts(String... deptIds) {
        this.deptService.deleteDepts(deptIds);
        return Message.ok("删除部门成功");
    }

    @RestRequestMapping(method = RequestMethod.PUT, permits = "dept:update", description = "修改部门")
    public Message updateDept(Dept dept) {
        this.deptService.updateDept(dept);
        return Message.ok("修改部门成功");
    }

    @RestRequestMapping(value = "/excel", method = RequestMethod.POST, permits = "dept:export", description = "导出部门数据")
    public void export(Dept dept, HttpServletResponse response) {
        try {
            List<Dept> depts = this.deptService.findDeptList(dept);
            super.downXlsx("部门信息表", depts, response);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败", e);
        }
    }
}